const serviceCommon=require('../utils/serviceCommon');
const UploadService=require('../utils/UploadService');
const UserDao=require('../dao/UserDao');
const VerificationDao=require('../dao/VerificationDao');
const tokenservice=require('./tokenservice');
const rendom=require('../utils/rendom');

const server={
    /**
     * 教师账号密码登录
     * */
    Login:async(data)=>{
        let result;
        let res = await serviceCommon.searchMethod(UserDao,'Login',data);
        if(res.code===1){
            let UserId=res.data[0].TeacherUserId;
            let Token=await rendom.createUUID();
            let datas={UserId:UserId,Token:Token};
            let re=await tokenservice.updateToken(datas);
            if(re.code===1){
                res.data[0].Token=Token;
                result=res;
            }else {
                result={code:2,msg:'Token生成失败'}
            }
        }else {
            result=res
        }
        return result;
    },
    /**
     * 教师手机号发送验证码(登录发送)
     * */
    TeacherSendVerification:async(data)=>{
        let result;
        let res = await serviceCommon.searchMethod(UserDao,'InspectTeacherUserMobile',data);
        if(res.code===1){
            let VerificationCode=await rendom.RandomInt();
            let Mobile=data.TeacherUserMobile;
            let datas={
                VerificationCode:VerificationCode,
                Mobile:Mobile
            };
            let re = await serviceCommon.updateMethod(VerificationDao,'UpdateVerification',datas);
            if(re.code===1){
                result={code:1,msg:'发送成功'}
            }else {
                result={code:2,msg:'验证码存入失败'}
            }
        }else {
            result={code:3,msg:'该账号不存在'}
        }
        return result
    },
    /**
     * 教师修改昵称
     * */
    UpdateTeacherUserName:async(data)=>{
        let res = await serviceCommon.updateMethod(UserDao,'UpdateTeacherUserName',data);
        return res
    },
    /**
     * 教师修改手机号
     * */
    UpdateTeacherUserMobile:async(data)=>{
        let res = await serviceCommon.updateMethod(UserDao,'UpdateTeacherUserMobile',data);
        return res
    },
    /**
     * 教师修改密码并做密码验证
     * */
    UpdatePassword:async (data)=>{
        let result;
        let PasswordVerificationData={
            TeacherUserId:data.TeacherUserId,
            TeacherUserPassword:data.OldTeacherUserPassword
        };
        let res=await serviceCommon.searchMethod(UserDao,'PasswordVerification',PasswordVerificationData);
        if(res.code===1){
            let UpdatePasswordData={
                TeacherUserId:data.TeacherUserId,
                TeacherUserPassword:data.NewTeacherUserPassword
            };
            let re=await serviceCommon.updateMethod(UserDao,'UpdatePassword',UpdatePasswordData);
            if(re.code===1){
                result={code:1,msg:'修改成功'}
            }else {
                result={code:3,msg:'修改失败'}
            }
        }else {
            result={code:2,msg:'密码错误'}
        }
        return result
    },
    /**
     * 修改教师头像
     * */
    UpdateTeacherUserAvatar:async (data)=>{
        let result;
        let OldPath='./public/imgData/'+data.Date+'/'+data.FileName;
        let Path='./public/images/User/Avatar/'+data.FileName;
        let res=await UploadService.UpdateLoc(OldPath,Path);
        if(res.code===1){
            let UpdateTeacherUserAvatarData={
                TeacherUserAvatar:'http://127.0.0.1:3000/images/User/Avatar/'+data.FileName,
                TeacherUserId:data.TeacherUserId
            };
            let re=await serviceCommon.updateMethod(UserDao,'UpdateTeacherUserAvatar',UpdateTeacherUserAvatarData);
            if(re.code===1){
                result={code:1,msg:'图片修改成功'}
            }else {
                result={code:3,msg:'图片修改失败'}
            }
        }else {
            result={code:2,msg:'文件移动失败'}
        }
        return result
    },
    /**
     * 教师升级条件查询
     * */
    QueryUpgrade:async (data)=>{
        let result=await serviceCommon.searchMethod(UserDao,'QueryUpgrade',data);
        return result
    },
    /**
     * 根据用户id查询教师信息
     * */
    QueryTeacherUser:async (data)=>{
        let result=await serviceCommon.searchMethod(UserDao,'QueryTeacherUser',data);
        return result
    }
};

module.exports=server;
